﻿#include "SetDeviceCmd.h"
#include "../util/JsonUtil.h"
#include "../util/DateTime.h"

SetDeviceCmd::SetDeviceCmd()
{
}

SetDeviceCmd::~SetDeviceCmd()
{
}

bool SetDeviceCmd::Parse(std::string json)
{
	// 解析json
	JsonUtil jsonUtil;
	if (jsonUtil.Parse(json))
	{
		std::string id = jsonUtil.GetString("device.id", "");
		std::string name = jsonUtil.GetString("device.name", "");
		int enabled = jsonUtil.GetInt("device.enabled", 1);
		std::string rtspUrl = jsonUtil.GetString("device.rtspUrl", "");
		std::string username = jsonUtil.GetString("device.username", "");
		std::string password = jsonUtil.GetString("device.password", "");
		std::string manufacture = jsonUtil.GetString("device.manufacture", "");
		std::string startTime = jsonUtil.GetString("device.startTime", "00:00:00");
		std::string stopTime = jsonUtil.GetString("device.stopTime", "23:59:59");
		std::string params = jsonUtil.GetString("device.params", "");
		int silence = jsonUtil.GetInt("device.silence", 5);
		int frameGap = jsonUtil.GetInt("device.frameGap", 50);
		std::string createTime = jsonUtil.GetString("device.createTime", "");
		std::string updateTime = jsonUtil.GetString("device.updateTime", "");
		
		m_device.SetId(id);
		m_device.SetName(name);
		m_device.SetEnabled(enabled);
		m_device.SetRtspUrl(rtspUrl);
		m_device.SetUsername(username);
		m_device.SetPassword(password);
		m_device.SetManufacture(manufacture);
		m_device.SetStartTime(startTime);
		m_device.SetEndTime(stopTime);
		m_device.SetParams(params);
		m_device.SetSilence(silence);
		m_device.SetFrameGap(frameGap);
		DateTime time = DateTime::Now();
		time.From(createTime);
		m_device.SetCreateTime(time);
		time.From(updateTime);
		m_device.SetUpdateTime(time);
		return true;
	}
	return false;
}

DeviceInfo SetDeviceCmd::GetDevice()
{
	return m_device;
}